﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreeGraphicalControl;
using Collections;
using FeatureModelTool.ProjectView.ProjectTreeStructures;
using Collections.TreeCollections;
using System.Windows.Controls;
using FeatureModelTool.ProjectView.MenuItems;
using FeatureModelTool.Windows;
using System.Windows;
using WinForms = System.Windows.Forms;
using System.IO;
using System.Windows.Media;
using System.Diagnostics;

namespace FeatureModelTool.ProjectView.TreeItems
{
    public class TreeItem_ProjectTreeRoot : TreeViewItemControl
    {
        #region Properties
        public ProjectTreeRoot ProjectTreeInstance { get; protected set; } 
        #endregion

        private readonly static string _ICON_SOURCE = "/FeatureModelTool;component/Images/solution.png";

        public TreeItem_ProjectTreeRoot(ProjectTreeRoot root)
            : base()
        {
            ProjectTreeInstance = root;
            this.Header = MainWindow.GenerateTreeItemHeader(root.Name, _ICON_SOURCE
                , Brushes.White);
        }

        /// <summary>
        /// Tree Item Construct
        /// </summary>
        /// <param name="treeRoot"></param>
        /// <returns></returns>
        public static TreeViewItemControl ConstructTreeItem(TreeRoot treeRoot)
        {
            ProjectTreeRoot projRoot = treeRoot as ProjectTreeRoot;
            if (projRoot == null)
            {
                return null;
            }
            return new TreeItem_ProjectTreeRoot(projRoot);
        }

        #region Context Menu
        /// <summary>
        /// Rename
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void Rename_EventHandler(object sender, RoutedEventArgs e)
        {
            if (sender is MenuItem_File)
            {
                MenuItem_File file = (MenuItem_File)sender;
                string orgName = file.File.FileName;
                RenameDialog dialog = new RenameDialog(orgName);
                dialog.ShowDialog();

                if (dialog.Dirty == true)
                {
                    string newName = dialog.NewName;
                    string orgFullPath = file.File.FullFilePath;
                    file.File.FileName = newName;
                    string newFullPath = file.File.FullFilePath;
                    File.Move(orgFullPath, newFullPath);
                }
            }
            else if (sender is MenuItem_Folder)
            {
            }
            else if (sender is MenuItem_Root)
            {
            }
        }
        /// <summary>
        /// New Folder
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void NewFolder_EventHandler(object sender, RoutedEventArgs e)
        {
            INonLeaf nonLeaf = null;
            ProjectTree projectTree = null;
            if (sender is MenuItem_Root)
            {
                MenuItem_Root root = (MenuItem_Root)sender;
                nonLeaf = root.ProjRoot;
                projectTree = root.Proj;
            }
            else if (sender is MenuItem_Folder)
            {
                MenuItem_Folder folder = (MenuItem_Folder)sender;
                nonLeaf = folder.Folder;
                projectTree = folder.Proj;
            }
            else
            {
                return;
            }
            // get folder name
            NewFolderDialog dia = new NewFolderDialog();
            dia.ShowDialog();
            if (dia.Dirty == false)
            {
                return;
            }

            try
            {
                projectTree.CreateNewFolder(nonLeaf, dia.FolderName, true);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message + exp.StackTrace);
            }
            //projectTree.CreateNewInstance(typeof(ProjectFolder), nonLeaf, dia.FolderName, true);
        }
        /// <summary>
        /// New Feature Model
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void NewFile_EventHandler(object sender, RoutedEventArgs e)
        {
            INonLeaf nonLeaf = null;
            ProjectTree projectTree = null;
            if (sender is MenuItem_Root)
            {
                MenuItem_Root root = (MenuItem_Root)sender;
                nonLeaf = root.ProjRoot;
                projectTree = root.Proj;
            }
            else if (sender is MenuItem_Folder)
            {
                MenuItem_Folder folder = (MenuItem_Folder)sender;
                nonLeaf = folder.Folder;
                projectTree = folder.Proj;
            }
            else
            {
                return;
            }
            // get folder name
            NewFileDialog dia = new NewFileDialog();
            dia.ShowDialog();
            if (dia.Dirty == false)
            {
                return;
            }

            try
            {
                projectTree.CreateNewFile(nonLeaf, dia.FileName, true);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message + exp.StackTrace);
            }
            //projectTree.CreateNewInstance(typeof(ProjectFile), nonLeaf, dia.FileName, true);
        }
        /// <summary>
        /// Save Project
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void SaveProject_EventHandler(object sender, RoutedEventArgs e)
        {
            MenuItem_Root root = sender as MenuItem_Root;
            if (root == null)
            {
                return;
            }
            try
            {
                root.Proj.Save();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message + exp.StackTrace);
            }
        }
        /// <summary>
        /// Add Existed File
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void AddExistedFile_EventHandler(object sender, RoutedEventArgs e)
        {
            ProjectTree projTree = null;
            INonLeaf nonLeaf = null;
            if (sender is MenuItem_Root)
            {
                MenuItem_Root root = (MenuItem_Root)sender;
                projTree = (ProjectTree)root.Proj;
                nonLeaf = root.ProjRoot;
            }
            else if (sender is MenuItem_Folder)
            {
                MenuItem_Folder folder = (MenuItem_Folder)sender;
                projTree = (ProjectTree)folder.Proj;
                nonLeaf = folder.Folder;
            }
            else
            {
                return;
            }

            WinForms.OpenFileDialog dialog = new WinForms.OpenFileDialog();
            dialog.Filter = "Feature Model File (.fm)|*.fm|All Files (*.*)|*.*";
            WinForms.DialogResult result = dialog.ShowDialog();
            if (result == WinForms.DialogResult.OK)
            {
                try
                {
                    projTree.AddExistFile(nonLeaf, dialog.FileName);
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message + exp.StackTrace);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void OpenInWinExplorer_EventHandler(object sender, RoutedEventArgs e)
        {
            if(sender is MenuItem_Root)
            {
                MenuItem_Root root = (MenuItem_Root)sender;
                try
                {
                    Process.Start("explorer.exe", "/select, " + root.ProjRoot.FullPath);
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message, exp.StackTrace);
                }
            }
        }
        /// <summary>
        /// Context Menu
        /// </summary>
        /// <param name="tree"></param>
        /// <returns></returns>
        public static ContextMenu ContextGenerator(TreeViewItemControl treeItem, TreeRoot treeRoot)
        {
            ProjectTreeRoot projRoot = treeRoot as ProjectTreeRoot;
            TreeItem_ProjectTreeRoot treeItem_Root = treeItem as TreeItem_ProjectTreeRoot;
            if (projRoot == null
                || treeItem_Root == null)
            {
                return null;
            }
            ContextMenu menu = new ContextMenu();

            // new folder
            MenuItem_Root newFolder = new MenuItem_Root(treeItem_Root, projRoot)
            {
                Name = projRoot.ID.ToString(),
                Header = "New Folder"
            };
            newFolder.Click += new RoutedEventHandler(NewFolder_EventHandler);

            // new file
            MenuItem_Root newFeatureModel = new MenuItem_Root(treeItem_Root, projRoot)
            {
                Name = projRoot.ID.ToString(),
                Header = "New File"
            };
            newFeatureModel.Click += new RoutedEventHandler(NewFile_EventHandler);

            // add exist file
            MenuItem_Root addFile = new MenuItem_Root(treeItem_Root, projRoot)
            {
                Name = projRoot.ID.ToString(),
                Header = "Add Existed File",
            };
            addFile.Click += new RoutedEventHandler(AddExistedFile_EventHandler);

            // save project
            MenuItem_Root save = new MenuItem_Root(treeItem_Root, projRoot)
            {
                Name = projRoot.ID.ToString(),
                Header = "Save",
            };
            save.Click += new RoutedEventHandler(SaveProject_EventHandler);

            // rename
            MenuItem_Root rename = new MenuItem_Root(treeItem_Root, projRoot)
            {
                Name = projRoot.ID.ToString(),
                Header = "Rename",
            };
            rename.Click += new RoutedEventHandler(Rename_EventHandler);

            // open folder in windows explorer
            MenuItem_Root openInWinExplorer = new MenuItem_Root(treeItem_Root, projRoot)
            {
                Name = "OpenInWinExplorer",
                Header = "Open In Windows Explorer"
            };
            openInWinExplorer.Click += new RoutedEventHandler(OpenInWinExplorer_EventHandler);

            menu.Items.Add(newFolder);

            menu.Items.Add(new Separator());
            menu.Items.Add(addFile);
            menu.Items.Add(newFeatureModel);

            menu.Items.Add(new Separator());
            menu.Items.Add(save);

            menu.Items.Add(new Separator());
            menu.Items.Add(rename);
            menu.Items.Add(openInWinExplorer);

            return menu;
        }
        #endregion

    }
}
